package jmind.core.test;

import java.util.List;
import java.util.Map;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class Guava {

    public static void main(String[] args) throws InterruptedException {
        Map<String, String> map = Maps.newHashMap();
        Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(12).softValues().build();
        cache.put("abcde", "fedgr");
        for (int i = 0; i < 10; i++) {
            cache.put("ddddddddd" + i, "cccccc" + i);
        }
        int i = 1;
        while (true) {
            map.put("aaaaaaa" + i, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" + i);
            System.out.println(cache.size() + "----------" + cache.getIfPresent("abcde"));
            System.out.println(cache.asMap().keySet().size() + "=" + cache.asMap().values().size());
            System.out.println(cache.asMap());
            Thread.sleep(3000l);
        }

    }

    /**
     * list转换为字符串
     */
    public void joinTest() {
        List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
        String result = Joiner.on(",").join(names);

    }

    /**
     * map转换为字符串
     */
    public void whenConvertMapToString_thenConverted() {
        Map<String, Integer> salary = Maps.newHashMap();
        salary.put("John", 1000);
        salary.put("Jane", 1500);
        String result = Joiner.on(" , ").withKeyValueSeparator(" = ").join(salary);
        System.out.println(result);
    }

    /**
     * list转String，跳过null
     */
    public void whenConvertListToStringAndSkipNull_thenConverted() {
        List<String> names = Lists.newArrayList("John", null, "Jane", "Adam", "Tom");
        String result = Joiner.on(",").skipNulls().join(names);
        System.out.println(result);
    }

    /**
     * list转String，将null变成其他值
     */
    public void whenUseForNull_thenUsed() {
        List<String> names = Lists.newArrayList("John", null, "Jane", "Adam", "Tom");
        String result = Joiner.on(",").useForNull("nameless").join(names);

        System.out.println(result);
    }

    /**
     * String to List
     */
    public void whenCreateListFromString_thenCreated() {
        String input = "apple - banana - orange";
        List<String> result = Splitter.on("-").trimResults().splitToList(input);
        System.out.println(result);
        //assertThat(result, contains("apple", "banana", "orange"));
    }

    /**
     * String to Map
     */
    public void whenCreateMapFromString_thenCreated() {
        String input = "John=first,Adam=second";
        Map<String, String> result = Splitter.on(",").withKeyValueSeparator("=").split(input);

    }

    /**
     * 多个字符进行分割
     */
    public void whenSplitStringOnMultipleSeparator_thenSplit() {
        String input = "apple.banana,,orange,,.";
        List<String> result = Splitter.onPattern("[.|,]").omitEmptyStrings().splitToList(input);
        System.out.println(result);
    }

    /**
     * 每隔多少字符进行分割
     */
    public void whenSplitStringOnSpecificLength_thenSplit() {
        String input = "Hello world";
        List<String> result = Splitter.fixedLength(3).splitToList(input);
        System.out.println(result);
    }

    /**
     * 限制分割多少字后停止
     */
    public void whenLimitSplitting_thenLimited() {
        String input = "a,b,c,d,e";
        List<String> result = Splitter.on(",").limit(4).splitToList(input);

        System.out.println(result);
    }

}
